package org.redr.widgetGUI.settings;

import java.util.List;

import org.w3c.dom.Element;


/**
 * <p>The WidgetSettings class is the principal way of setting settings for RedRQTCore 
 * widgets.  All WidgetState children are able to accept an element of WidgetSettings
 * and this is used to set the default settings of that widget.  All RedRWidgets are
 * passed a WidgetSettings object on initialization which is used to control the saved 
 * state of all qtWidgets in that widget.
 * 
 * In general developers should use the 
 * .setDefault() setting as follows;
 * <code>
 * WidgetSettings myWidgetSettings = widgetSettings.getSettings("myWidgetStateChild");
 * myWidgetSettings.setDefault("orientation", "horizontal");
 * myWidgetSettings.setDefault("toolTip", "This is a tooltip to show to you");
 * myWidgetSettings.items("itemGroup")
 * 		.setDefaultItems("Item1||Some item", "Item2||Some other item");
 * WidgetStateChild myWidget = new WidgetStateChild(this.controlArea(), myWidgetSettings);
 * </code>
 * </p><p>
 * Developers may be able to set many different settings depending on the WidgetStateChild that is 
 * being set.  WidgetStateChildren must me able to set any default setting, that is they all 
 * come with predefined defaults.  Setting unused defaults will be ignored if unused by the WidgetStateChild.
 * </p>
 * @author Kyle R Covington
 *
 */
public class WidgetSettings extends XMLSetting{
	protected String childType = "QTWidgetSetting";
	public WidgetSettings(Element el){
		e	=	el;
	}
	
	public WidgetSettings getSettings(String id){
		Element ce = getChildElement(childType, id);
		
		if (ce == null){
			Element newE = e.getOwnerDocument().createElement(childType);
			newE.setAttribute("id", id);
			e.appendChild(newE);
			return new WidgetSettings(newE);
		} else {
			return new WidgetSettings(ce);
		}
	}
	
	public RVariableSettings getRVariableSettings(){
		Element newE = getChildElement("Rvariables", null);
		if (newE == null){
			newE = e.getOwnerDocument().createElement("RVariables");
			e.appendChild(newE);
			return new RVariableSettings(newE);
		} else return new RVariableSettings(newE);
	}
	
	public void addRVariables(List<String> variables, String widgetID){
		RVariableSettings varSettings = getRVariableSettings();
		for (String v : variables){
			varSettings.setSetting(v, v + widgetID);
		}
	}
	
	public String getRVariable(String id){
		RVariableSettings varSettings = getRVariableSettings();
		return varSettings.getSetting(id);
	}
}
